package dshop.model.push;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

import appbase.jfinal.ext.BaseModel;
import appbase.jfinal.ext.ModelKit;

public class PushRecord extends BaseModel<PushRecord> {

	private static final long serialVersionUID = -6967680902549117464L;

	public static final PushRecord dao = new PushRecord();

	// 1 采购信息
	public static final int CONTENT_REF_TYPE_PROCUREMENT = 1;

	public static PushRecord create(int contentRefId, int contentRefType, int pushUserId, int pushCnt) {
		PushRecord r = new PushRecord();
		r.set("contentRefId", contentRefId).set("contentRefType", contentRefType).set("pushUserId", pushUserId)
				.set("pushCnt", pushCnt).set("createTime", new Date());
		return r;
	}

	/**
	 * @return [{ .., pushCnt }]
	 */
	public static void setPushCntToRs(List<Record> list, String contentRefIdColumn, int contentRefType) {
		dao.mergeCountNumToRs(list, contentRefIdColumn, "contentRefId", "pushCnt", "contentRefType", contentRefType);
	}

	public static List<Integer> listIdBy(int contentRefId, int contentRefType) {
		String sql = " select * from push_record where contentRefId = ? and contentRefType = ? ";
		List<PushRecord> list = dao.find(sql, contentRefId, contentRefType);
		return ModelKit.getToList(list, "id", Integer.class);
	}

	public static List<PushRecord> findByProcurementIds(List<Record> records) {
		if (records == null || records.isEmpty()) {
			return new ArrayList<PushRecord>();
		}
		StringBuilder idsBuilder = new StringBuilder();
		for (Record record : records) {
			idsBuilder.append(record.getInt("id")).append(",");
		}
		idsBuilder.deleteCharAt(idsBuilder.length()-1);
		String sql = " select * from push_record where contentRefId in ("+idsBuilder+") and contentRefType = ? ";
		return dao.find(sql, PushRecord.CONTENT_REF_TYPE_PROCUREMENT);
	}
	public static PushRecord findByProcurementId(int procurementId) {
		String sql = " select * from push_record where contentRefId = ? and contentRefType = ? ";
		return dao.findFirst(sql, procurementId, PushRecord.CONTENT_REF_TYPE_PROCUREMENT);
	}

}
